﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.eforceglobal.crux.BusinessFacades;
using com.eforceglobal.crux.configuration;
using com.eforceglobal.crux.configuration.FeatureManager;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.exceptions;
using com.eforceglobal.crux.logging;
using com.eforceglobal.crux.logging.Enums;
using com.eforceglobal.crux.security;
using com.eforceglobal.crux.security.EncryptionAndHashing.Enums;
using com.eforceglobal.crux.views;

namespace com.eforceglobal.crux.controllers
{
    public class ForgotPasswordController
    {
        IForgotPassword _view = null;
        public ForgotPasswordController(IForgotPassword view)
        {
            _view = view;
        }

        public void InitView(bool IsPostBack)
        {
            if (!IsPostBack)
            {
                ForgotPasswordFacade facade = new ForgotPasswordFacade();
                _view.SecurityQuestionList = facade.GetAllSecurityQuestions();
            }
        }

        public void NavigateToLoginPage()
        {
            NavigationController.OpenView("Login");
        }

        public static PasswordStorageType GetPasswordStorageType()
        {
            return CruxConfigurationManager.GetPasswordStorageType();
        }

        public void ResetPassword()
        {
            PasswordStorageType passwordStorageType = CruxConfigurationManager.GetPasswordStorageType();
            int savedPasswordCount =
                Convert.ToInt32(CruxConfigurationManager.GetApplicationCustomSetting("NumberOfSavedPassword").Value);
            IFeatureManager featureManager = FeatureManagerFactory.GetFeatureManager();
            bool isFeatureSupported = featureManager.IsFeatureSupported("SavePreviousPasswords");
            ForgotPasswordFacade facade = new ForgotPasswordFacade();
            ICruxSecurityManager cruxSecurityManager = new CruxSecurityManager();
            bool isValidUserName = cruxSecurityManager.ValidateUser(_view.UserName);
            if (isValidUserName)
            {
                bool isValid = facade.ValidateDetails(_view.UserName, _view.SelectedSecurityQuestionId, _view.Answer);
                if (isValid)
                {
                    try
                    {
                        facade.ResetPassword(_view.UserName, passwordStorageType, savedPasswordCount, isFeatureSupported);
                        _view.EnableMailSentMessage = true;
                        _view.ErrorDetails = null;
                    }
                    catch (Exception ex)
                    {
                        ApplicationLogger.LogApplicationMessage(
                            ex.Message, ApplicationLogType.Database, ex.StackTrace);
                        _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
                    }
                }
                else
                    _view.ErrorDetails = CruxConfigurationManager.GetErrorInformation("911");
            }
            else
                _view.ErrorDetails = CruxConfigurationManager.GetErrorInformation("913");
        }
    }
}
